# Contributor: Eivind Uggedal <eu@eju.no>
# Contributor: Jake Buchholz <tomalok@gmail.com>
# Maintainer: Jake Buchholz <tomalok@gmail.com>
pkgname=docker
pkgver=19.03.12
_gitcommit=48a66213fe1747e8873f849862ff3fb981899fc6 	# https://github.com/docker/docker-ce/commits/v$pkgver
pkgrel=0
pkgdesc="Pack, ship and run any application as a lightweight container"
url="https://www.docker.io/"
arch="all !mips !mips64" # go is missing
license="Apache-2.0"
depends="docker-engine docker-cli"
makedepends="go go-md2man btrfs-progs-dev bash linux-headers coreutils lvm2-dev libtool
	libseccomp-dev"
install="$pkgname.pre-install"

# from components/engine/vendor.conf:
#    grep libnetwork components/engine/vendor.conf
#    grep cobra components/engine/vendor.conf
_libnetwork_ver=026aabaa659832804b01754aaadd2c0f420c68b6
_cobra_ver="0.0.3"

# secfixes:
#   19.03.11-r0:
#     - CVE-2020-13401
#   19.03.1-r0:
#     - CVE-2019-14271
#   18.09.8-r0:
#     - CVE-2019-13509
#   18.09.7-r0:
#     - CVE-2018-15664

subpackages="
	$pkgname-engine:engine
	$pkgname-openrc:engine_openrc:noarch
	$pkgname-cli:cli
	$pkgname-doc:cli_doc:noarch
	$pkgname-bash-completion:cli_bashcomp:noarch
	$pkgname-fish-completion:cli_fishcomp:noarch
	$pkgname-zsh-completion:cli_zshcomp:noarch
	$pkgname-vim:cli_vim:noarch
	"

source="
	docker-$pkgver.tar.gz::https://github.com/docker/docker-ce/archive/v$pkgver.tar.gz
	libnetwork-$_libnetwork_ver.tar.gz::https://github.com/docker/libnetwork/archive/$_libnetwork_ver.tar.gz
	cobra-$_cobra_ver.tar.gz::https://github.com/spf13/cobra/archive/v$_cobra_ver.tar.gz
	docker.initd
	docker.confd
	netns-mips64-fork.patch
	stat-struct-fixes-mips64.patch
	configurable_buildmode.patch
	"

builddir="$srcdir"/docker-ce-$pkgver
_cli_builddir="$builddir"/components/cli
_daemon_builddir="$builddir"/components/engine
_buildtags="seccomp"

_libnetwork_builddir="$srcdir"/libnetwork-$_libnetwork_ver

case "$CARCH" in
	mips64*) _buildmode=exe ;;
	riscv64) _buildmode=exe ;;
	*) _buildmode=pie ;;
esac

build() {
	export AUTO_GOPATH=1
	export GITCOMMIT=$_gitcommit		# for cli
	export DOCKER_GITCOMMIT=$_gitcommit	# for engine
	export DOCKER_BUILDTAGS=$_buildtags
	unset CC # prevent possible ccache issues

	# libnetwork (docker-proxy)
	msg "building docker-proxy"
	cd "$_libnetwork_builddir"
	mkdir -p src/github.com/docker/
	ln -s "$_libnetwork_builddir" src/github.com/docker/libnetwork
	GOPATH="$PWD" go build -v -ldflags="-linkmode=external" -o docker-proxy github.com/docker/libnetwork/cmd/proxy

	# daemon
	msg "building daemon"
	cd "$_daemon_builddir"
	mkdir -p src/github.com/docker/
	ln -s "$_daemon_builddir" src/github.com/docker/docker
	GOPATH="$PWD" VERSION="$pkgver" hack/make.sh dynbinary

	# Required for building man-pages
	export GOPATH="$_cli_builddir"
	export GOBIN="$GOPATH/bin"
	export PATH="$GOBIN:$PATH"

	# cli
	msg "building cli"
	cd "$_cli_builddir"
	mkdir -p "$GOPATH"/src/github.com/docker/
	ln -s "$_cli_builddir" "$GOPATH"/src/github.com/docker/cli
	LDFLAGS="" make VERSION="$pkgver" BUILDMODE=$_buildmode dynbinary

	# docker man
	msg "building docker man pages"
	cd "$srcdir"
	# cobra
	mkdir -p "$GOPATH"/src/github.com/spf13/
	ln -sf "$PWD"/cobra-$_cobra_ver "$GOPATH"/src/github.com/spf13/cobra

	# convert md to man pages
	msg "generating man pages"
	cd "$_cli_builddir"
	# make manpages (from cli/Makefile, but using installed go-md2man)
	mkdir -p ./man/man1
	# Generate man pages from cobra commands
	go build -o /tmp/gen-manpages github.com/docker/cli/man
	/tmp/gen-manpages --root "$(pwd)" --target "$(pwd)/man/man1"
	# generate legacy manpages from markdown
	./man/md2man-all.sh -q
}

# docker itself is a meta package
package() {
	mkdir -p "$pkgdir"
}

engine() {
	pkgdesc="Docker Engine (dockerd)"
	depends="ca-certificates containerd iptables tini-static"

	install -Dm755 "$_daemon_builddir"/bundles/dynbinary-daemon/dockerd \
		"$subpkgdir"/usr/bin/dockerd

	install -Dm755 "$_libnetwork_builddir"/docker-proxy \
		"$subpkgdir"/usr/bin/docker-proxy

	# symlink externally provided tini-static binary
	ln -s /sbin/tini-static "$subpkgdir"/usr/bin/docker-init
}

engine_openrc() {
	pkgdesc="OpenRC init scripts for Docker"
	depends=""
	install_if="openrc $pkgname-engine=$pkgver-r$pkgrel"

	install -Dm755 "$srcdir"/docker.initd "$subpkgdir"/etc/init.d/docker
	install -Dm644 "$srcdir"/docker.confd "$subpkgdir"/etc/conf.d/docker
}

cli() {
	pkgdesc="Docker CLI"
	depends="ca-certificates"

	# 'build/docker' is a symlink to 'docker-linux-$arch' e.g. 'docker-linux-amd64'
	install -Dm755 "$_cli_builddir"/build/docker \
		"$subpkgdir"/usr/bin/docker
}

cli_doc() {
	pkgdesc="Documentation for Docker"
	depends=""
	install_if="docs $pkgname-cli=$pkgver-r$pkgrel"

	mkdir -p "$subpkgdir"/usr/share/man/man1
	gzip -9 "$_cli_builddir"/man/man1/*
	install -Dm644 "$_cli_builddir"/man/man1/* \
		"$subpkgdir"/usr/share/man/man1
}

cli_bashcomp() {
	pkgdesc="Bash completion for Docker"
	depends=""
	install_if="bash-completion $pkgname-cli=$pkgver-r$pkgrel"

	install -Dm644 "$_cli_builddir"/contrib/completion/bash/$pkgname \
		"$subpkgdir"/usr/share/bash-completion/completions/$pkgname
}

cli_fishcomp() {
	pkgdesc="Fish shell completion for Docker"
	depends=""
	install_if="fish<3 $pkgname-cli=$pkgver-r$pkgrel" # fish above version 3 has docker completion

	install -Dm644 "$_cli_builddir"/contrib/completion/fish/$pkgname.fish \
		"$subpkgdir"/usr/share/fish/completions/$pkgname.fish
}

cli_zshcomp() {
	pkgdesc="Zsh completion for Docker"
	depends=""
	install_if="zsh $pkgname-cli=$pkgver-r$pkgrel"

	install -Dm644 "$_cli_builddir"/contrib/completion/zsh/_$pkgname \
		"$subpkgdir"/usr/share/zsh/site-functions/_$pkgname
}

cli_vim() {
	pkgdesc="Vim syntax for Docker"
	depends=""
	install_if="vim $pkgname-cli=$pkgver-r$pkgrel"

	local f=
	for f in ftdetect/dockerfile.vim syntax/dockerfile.vim; do
		install -Dm644 "$_daemon_builddir"/contrib/syntax/vim/$f \
			"$subpkgdir"/usr/share/vim/vimfiles/$f
	done
}

sha512sums="6ede7e0c5494882d88dae5b4ae45049937dfc65b9d655988ae4c0d6f9759bcac8799251f37a3e4ce49cdfbb04e31f9a6a05d131fb5005b382d2306ca0d5fcfa3  docker-19.03.12.tar.gz
fe8644611e975c051ee6e7ad4871624fb45862d9b4a2fd62ea4283e76ad9804d91d585e2165915b09356f11f9dfb31c40dd9ce6a66d63c5032c8c62354960538  libnetwork-026aabaa659832804b01754aaadd2c0f420c68b6.tar.gz
c38db9432a168f913b41a1e1b11d84bedfade82ff70791be9d343a6cc86b8a05b18bae344d67ebd8bae4c98662db7ac664a9dc86fa9b9ad4aa5c96cbf0178efb  cobra-0.0.3.tar.gz
e3a882a732f498ea62a770e1d91cfb910d4206156dd9a8546cf6af94dcb9c6201e0f712a86e3ad352fbae4f0eedb2b4055976af69385c671dc8e9a43b78bc31b  docker.initd
f25523f43376ccef71a49618e556e0a16db3acad29eb09fe86c4e572562bdea0bc1eabab00159278835ad9d7c007f2cd10b2ed31f7213b0d9074582dc80a976f  docker.confd
2bdc5c226b7ebefe93bbe6b2900c259de43ee1fb49ac527966942b262e72f4ca441fecc7406c6c737f5c9694fc0141ba5e779bc7d269c8e644553efd89862b01  netns-mips64-fork.patch
89b047fe8379d230572fba120ea8d5784a26467d9239558e77e29a8c1b4bd78d55d12c8fe5969dafd981a551c8c2b640b747fe3c4d89adc438c4a7911ea94aa0  stat-struct-fixes-mips64.patch
04100dbeb03009beb0f4bdff5f7af481c5668c8e8d2f952603c78a51cb7551b671c980a321b058d2c570cea07e7213b1dab359dedb6907215eff33b7e5181887  configurable_buildmode.patch"
